import { defineStore } from "pinia";
import { Names } from "./store-name";
import { useRequest } from "alova";
import { addAuthAction, deleteAuthAction, queryAuthActionList } from "@/api/authAction";
import type { TCreateAuthActionReq, TAuthActionItem, TAuthActionListRes, TMethodsList } from "@/typing/authAction";

export const useAuthActionStore = defineStore(Names.AUTH_ACTION, {
  state() {
    return {
      isLoading: false,
      authActionList: <TAuthActionItem[]>[],
      methodList: <TMethodsList>[]
    }
  },
  getters: {

  },
  actions: {
    queryAuthActionList(businessKey: string, { force }: { force?: boolean } = { force: false }) {
      this.isLoading = true
      const { onSuccess } = useRequest(queryAuthActionList(businessKey), { force: force });
      onSuccess(({ data }) => {
        const { data: { apiList, methodList } } = data as TAuthActionListRes
        this.authActionList = apiList;
        this.methodList = methodList
        this.isLoading = false
      });
    },
    addAuthAction(req: TCreateAuthActionReq, fn: Function) {
      const { onSuccess } = useRequest(addAuthAction(req));
      onSuccess(({ data }) => {
        if (data) {
          fn && fn(data);
        }
      });
    },
    deleteAuthAction(id: string, fn: Function) {
      const { onSuccess } = useRequest(deleteAuthAction(id));
      onSuccess(({ data }) => {
        if (data) {
          fn && fn(data);
        }
      });
    },
  }
})
